﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Data.OleDb;
using Newtonsoft.Json;
using Nova_MOD;

public partial class DelStorage_DelData : System.Web.UI.Page
{
    Nova_DAL.DbHelper access = new DbHelper();
    Nova_DAL.DbHelper_ERP access_ERP = new DbHelper_ERP();
    string strSQL = "";
    DbCommand cmd = null;
    DataSet ds = null;
    public string strMaxStockNo = "";
    string strSec_Mat_ID = "";
    string strPID = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.hidden.Value != "" && this.hidden.Value != "undefined")//判断是否是模态关闭后刷新页面
        {
            txtStorageNo.Text = hidden.Value;
            bind();
            hidden.Value = "";
        }


        if (Session["StorageNov"] != null)
        {
            try
            {
                txtStorageNo.Text = Session["StorageNov"].ToString();
                Session["StorageNov"] = null;
            }
            catch
            {
            }
            bind();
        }

        if (!IsPostBack)
        {
            bind();
            //if (Request.Cookies["WType"].Value == "FSSC")
            //{
            //    ibtn_DR.Visible = false;
            //    ibtnxinjian.Visible = false;
            //    ibtnDelStock.Visible = false;
            //    ibtnAddList.Visible = false;
            //    ibtn_ERP.Visible = false;
            //}
            //else
            //{
            //    ibtn_DR.Visible = true;
            //    ibtnxinjian.Visible = true;
            //    ibtnDelStock.Visible = true;
            //    ibtnAddList.Visible = true;
            //    ibtn_ERP.Visible = true;
            //}
        }
    }

    //得到最大的单据号
    protected void getMaxStorageNo()
    {
        strSQL = "Select max(SUBSTRING(StockNo,3,8)) as StockNo From TBill_DelStorage Where Left(StockNo,2) = 'EB'";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds.Tables[0].Rows.Count > 0)
        {
            strMaxStockNo = ds.Tables[0].Rows[0]["StockNo"].ToString();
            if (strMaxStockNo == "")
            {
                strMaxStockNo = "EB00000001";
            }
            else
            {
                int iMaxStockNo = Convert.ToInt32(strMaxStockNo.ToString()) + 1;
                if (iMaxStockNo.ToString().Length == 1)
                {
                    strMaxStockNo = "EB0000000" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 2)
                {
                    strMaxStockNo = "EB000000" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 3)
                {
                    strMaxStockNo = "EB00000" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 4)
                {
                    strMaxStockNo = "EB0000" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 5)
                {
                    strMaxStockNo = "EB000" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 6)
                {
                    strMaxStockNo = "EB00" + iMaxStockNo.ToString();
                }
                else if (iMaxStockNo.ToString().Length == 7)
                {
                    strMaxStockNo = "EB0" + iMaxStockNo.ToString();
                }
                else
                {
                    strMaxStockNo = "EB" + iMaxStockNo.ToString();
                }
            }
        }
        else
        {
            strMaxStockNo = "EB00000001";
        }
        txtStorageNo.Text = strMaxStockNo.ToString();
    }

    public void bind()
    {
        //strSQL = "SELECT TAS.StockNo,TBM.S01,TBM.S02,TAS.Num,TAS.PID,TBM.Mat_Name,TBM.Mat_Code FROM TDelStorage_List TAS LEFT JOIN TBase_Material TBM ON TAS.Mat_Code = TBM.Mat_Code WHERE StockNo='" + txtStorageNo.Text + "' AND (TAS.TS02='" + WType + "' or 'FSSC'='" + WType + "')";

        strSQL = @"SELECT TDS.PID,TDS.StockNo,TBM.Mat_Code,TBM.MAT_NAME,TDS.Num,TDS.Act_Num,TBM.S01,TBM.S02,TBM.TM03,TBM.TM01,
                        TDS.TS01,TBM.Unit_Base FROM dbo.TDelStorage_List TDS 
                        LEFT JOIN dbo.TBase_Material TBM ON TBM.MAT_CODE=TDS.MAT_CODE
                        WHERE TDS.StockNo = '" + txtStorageNo.Text + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        gv.DataSource = access.ExecuteDataSet(cmd);
        gv.DataBind();
    }

    //protected void ibtnxinjian_Click(object sender, ImageClickEventArgs e)
    //{
    //    //lab_ERR.Text = "";
    //    getMaxStorageNo();
    //}
    protected void ibtnDelStock_Click(object sender, ImageClickEventArgs e)
    {
        getMaxStorageNo();
        //lab_ERR.Text = "";
        strSQL = "Select * From TBill_DelStorage where StockNo = '" + txtStorageNo.Text + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ShowBox.MessageBox(this.Page, "已有出库订单号！");
            return;
        }

        strSQL = "Insert into TBill_DelStorage(StockNo,Small_BillType,Contents,Bill_Status,OID,B01) values('" + txtStorageNo.Text + "','11','" + txtContents.Text + "','0','" + Request.Cookies["vNovaUserNo"].Value.ToString() + "',0)";
        cmd = access.GetSqlStringCommond(strSQL);
        int i = access.ExecuteNonQuery(cmd);
        if (i > 0)
        {

            ShowBox.MessageBox(this.Page, "出库单号添加成功！");
            gv.DataSource = null;
            gv.DataBind();
        }
        else
        {
            ShowBox.MessageBox(this.Page, "添加失败！");
        }
    }


    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        //lab_ERR.Text = "";
        if (txtStorageNo.Text == "")
        {
            ShowBox.MessageBox(this.Page, "请输入订单号！");
            return;
        }
        bind();
    }

    //protected void ibtnedit_Click(object sender, ImageClickEventArgs e)
    //{
    //    ImageButton ibtnE = (ImageButton)sender;
    //    ClientScript.RegisterStartupScript(GetType(), "info", "<script language='javascript'>document.getElementById('hidden').value=window.showModalDialog('DelDataPOP.aspx?MType=Edit&PID=" + ibtnE.CommandArgument.ToString() + "', window, 'dialogWidth=600px;dialogHeight=320px;scroll:no;resizable:no;status:0;');document.URL=location.href;</script>)");

    //}
    protected void ibtndelete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtndel = (ImageButton)sender;
        strSQL = "DELETE FROM TDelStorage_List WHERE PID = '" + ibtndel.CommandArgument.ToString() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        access.ExecuteNonQuery(cmd);
        bind();
    }

    protected void ibtnRe_Click(object sender, ImageClickEventArgs e)
    {
        //Response.Write("<script language='javascript'>windows.reload();</script>");
        Response.Redirect("DelData.aspx");
    }

    protected void ibtnedit_Click(object sender, ImageClickEventArgs e)
    {
        Session["StorageNov"] = txtStorageNo.Text;
        Random rnd = new Random();
        DateTime dtime = new DateTime();
        string stime = dtime.Date.ToString("yyyymmss") + rnd.Next(10000).ToString();

        ImageButton ibtnE = (ImageButton)sender;
        string[] sArray = ibtnE.CommandArgument.ToString().Split('|');
        //Page.ClientScript.RegisterStartupScript(GetType(), "info", "<script language='javascript'>document.getElementById('hidden').value=window.showModalDialog('Code_Edit.aspx?Romands=" + stime + "&MType=Edit&PID=" + sArray[0].ToString() + "&StorageNo=" + sArray[1].ToString() + "', window, 'dialogWidth=600px;dialogHeight=180px;scroll:no;resizable:no;status:0;');document.URL=location.href;</script>)");
        ClientScript.RegisterStartupScript(GetType(), "info", "<script language='javascript'>document.getElementById('hidden').value=window.open('DelDataPOP.aspx?Romands=" + stime + "&MType=Edit&PID=" + sArray[0].ToString() + "&StorageNo=" + sArray[1].ToString() + "', window, 'Height=230px,Width=600px,top=300, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;</script>)");
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

           // ImageButton lbtn_PID = (ImageButton)e.Row.FindControl("ibtnedit");
            //string updateUrl = string.Format("javascript: document.getElementById('{2}').value=window.showModalDialog('DelDataPOP.aspx?ram=" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + "&Mtype={0}&PID={1}', window, 'dialogWidth=600px;dialogHeight=180px;scroll:no;resizable:no;status:0;');document.URL=location.href;", "Edit", gv.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);
            //lbtn_PID.Attributes.Add("onclick", updateUrl);

            //lbtn_PID.Attributes.Add("onclick", "window.parent.opener.document.getElementById('txt_StorageNo').value='" + LabStock_No.Text.ToString() + "';parent.window.close();");

            //ImageButton ibtndelete = (ImageButton)e.Row.FindControl("ibtndelete");
            //if (Request.Cookies["WCode"].Value == "FSSC")
            //{

            //    lbtn_PID.Visible = false;
            //    ibtndelete.Visible = false;
            //}
            //else
            //{
            //    lbtn_PID.Visible = true;
            //    ibtndelete.Visible = true;

            //}
        }
    }
    //protected void ibtn_DR_Click(object sender, ImageClickEventArgs e)
    //{
    //    string fileName = this.File1.PostedFile.FileName;
    //    if (string.IsNullOrEmpty(fileName))
    //    {
    //        return;
    //    }
    //    string filter = fileName.Substring(fileName.LastIndexOf('.'));
    //    if (filter != ".xls")
    //    {
    //        ShowBox.MessageBox(this.Page, "请传入Excel表格！");
    //        return;
    //    }
    //    string filePath = Upload(File1);

    //    DataSet ds = LoadDataFromExcel(filePath);
    //    if (ds != null)
    //    {
    //        DataSetToSql(ds);
    //    }
    //}


    private DataSet LoadDataFromExcel(string filePath)
    {
        try
        {
            string strConn;
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;IMEX=1'";
            OleDbConnection OleConn = new OleDbConnection(strConn);
            OleConn.Open();

            //得到工作表的名称
            DataTable dt = OleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            string tablename = dt.Rows[0][2].ToString().Trim();

            if (tablename.Length > 0)
            {

                String sql = "SELECT * FROM  [" + tablename + "]";//可是更改Sheet名称，比如sheet2，等等    

                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                OleDaExcel.Fill(OleDsExcle, "Sheet1");
                OleConn.Close();
                return OleDsExcle;
            }
            return null;

        }
        catch (Exception err)
        {
            ShowBox.MessageBox(this.Page, "数据绑定Excel失败!失败原因：" + err.Message);

            return null;
        }
    }

    private string Upload(System.Web.UI.HtmlControls.HtmlInputFile file)
    {
        string excelPath = "";
        if (file.PostedFile.ContentLength.ToString() != "")
        {
            string[] strSplit = file.PostedFile.FileName.Split(new char[] { '.' });
            string strUp = strSplit[1].ToString();
            int ira = 0;
            int irb = 0;
            int irc = 0;
            Random ra = new Random();
            ira = ra.Next(10000);
            irb = ra.Next(10000);
            irc = ra.Next(10000);
            //获取文件名称
            string strName = System.DateTime.Now.ToString("yyyyMMddhhmmss").Replace("-", "").Replace(" ", "").Replace(":", "") + ira.ToString() + irb.ToString() + irc.ToString() + "." + strUp;

            string strUrl = "~/Upload/" + strName;
            file.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(strUrl));

            excelPath = HttpContext.Current.Request.PhysicalApplicationPath + "Upload\\" + strName;
        }
        return excelPath;
    }

    private void DataSetToSql(DataSet ds)
    {
        DataTable dt = ds.Tables[0];
        DateTime time = System.DateTime.Now;
        if (Convert.ToString(dt.Columns[0]) != "FSSC")
        {
            ShowBox.MessageBox(this.Page, "请先下载Excel模版，填写数据后在汇入！！");
            return;
        }
        StringBuilder sbMsg = new StringBuilder();
        if (string.IsNullOrEmpty(dt.Rows[2][2].ToString()))
        {
            sbMsg.Append("领料单号必须填写！");
        }
        else if (CheckExistStockNo(dt.Rows[2][2].ToString().Trim()))
        {
            sbMsg.Append("领料单号已经存在！");
        }

        if (!string.IsNullOrEmpty(dt.Rows[2][6].ToString()))
        {
            time = Convert.ToDateTime(dt.Rows[2][6].ToString());
        }
        for (int i = 4; i < dt.Rows.Count; i++)
        {
            if (string.IsNullOrEmpty(dt.Rows[i][1].ToString()) && string.IsNullOrEmpty(dt.Rows[i][4].ToString()))
            {
                break;
            }
            if (string.IsNullOrEmpty(dt.Rows[i][1].ToString()))
            {
                sbMsg.Append("物料编码必须填写！");
            }
            if (string.IsNullOrEmpty(dt.Rows[i][2].ToString()))
            {
                sbMsg.Append("名称与规格必须填写！");
            }
            //if (dt.Rows[i][2].ToString().Split('/').Length < 2)
            //{
            //    sbMsg.Append("名称与规格之间要加反斜杠 ！");
            //}
            try
            {
                decimal a = Convert.ToDecimal(dt.Rows[i][5].ToString());
            }
            catch
            {
                sbMsg.Append("应领数量必须为数字！");
            }
            if (sbMsg.Length > 0)
            {
                break;
            }

        }
        if (sbMsg.Length > 0)
        {
            ShowBox.MessageBox(this.Page, sbMsg.ToString());
            return;
        }

        strSQL = @"if NOT EXISTS(select top 1 1  from dbo.TBill_DelStorage where stockNO='" + dt.Rows[2][2].ToString() + @"') 

                   Insert into TBill_DelStorage(StockNo,Small_BillType,Contents,Bill_Status,OID,S01,CDT) values('" + dt.Rows[2][2].ToString() + "','11','','0','" + Request.Cookies["vNovaUserNo"].Value.ToString() + "','" + dt.Rows[2][4].ToString() + "','" + time + "')";
        cmd = access.GetSqlStringCommond(strSQL);
        access.ExecuteNonQuery(cmd);
        string strErr = "物料编码为：";
        int iFlag_Count = 0;
        for (int i = 4; i < dt.Rows.Count; i++)
        {
            if (string.IsNullOrEmpty(dt.Rows[i][1].ToString()) && string.IsNullOrEmpty(dt.Rows[i][4].ToString()))
            {
                break;
            }

            //如果物料不在立体仓库的物料资料中，则提示 并且不添加数据
            if (iCheckMaterail(dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString()) == 0)
            {
                iFlag_Count = 1;
                strErr += dt.Rows[i][1].ToString() + "  ";

            }
            else
            {
                Random rad = new Random();
                string strW01 = dt.Rows[2][2].ToString() + rad.Next(1000, 9999);
                HttpCookie WCode = Request.Cookies["WCode"];
                HttpCookie WName = Request.Cookies["WName"];

                strSQL = "Insert Into TDelStorage_List(StockNo,Mat_Code,Num,W01,S01,TS02,TS03) values('" + dt.Rows[2][2].ToString() + "','" + dt.Rows[i][1].ToString() + "','" + Convert.ToDecimal(dt.Rows[i][5].ToString()) + "','" + strW01 + "','" + dt.Rows[i][3].ToString() + "','" + WCode.Value.ToString().Trim() + "','" + HttpUtility.UrlDecode(WName.Value.ToString().Trim()) + "')";
                cmd = access.GetSqlStringCommond(strSQL);
                access.ExecuteNonQuery(cmd);
            }
        }
        if (iFlag_Count > 0)
        {
            strErr += "不存在立体库中。";
            //lab_ERR.Text = strErr.ToString();

            strSQL = "DELETE TBill_DelStorage WHERE STOCKNO NOT IN(SELECT STOCKNO FROM TDelStorage_List) AND STOCKNO='" + dt.Rows[2][2].ToString() + "'";
            cmd = access.GetSqlStringCommond(strSQL);
            access.ExecuteNonQuery(cmd);
        }

    }

    private int iCheckMaterail(string MatCode, string MatNameAndS01, string UnitBase)
    {
        int iCount = 0;
        strSQL = "select count(1) from dbo.TBase_Material where Mat_Code='" + MatCode + "' ";
        cmd = access.GetSqlStringCommond(strSQL);
        object obj = access.ExecuteScalar(cmd);

        string MatName = string.Empty;
        string S01 = string.Empty;

        iCount = Convert.ToInt32(obj);
        return iCount;
    }

    private bool CheckExistStockNo(string StockNo)
    {
        strSQL = @"select count(1) from dbo.TDelStorage_List where StockNo= '" + StockNo + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        return Convert.ToInt32(access.ExecuteScalar(cmd)) > 0 ? true : false;
    }


    protected void ibtn_ERP_Click(object sender, ImageClickEventArgs e)
    {
        int iCount = 0;
        int iFlag = 0;
        string strErr = "";

        strSQL = @"SELECT ID FROM TB_OrgCode";
        cmd = access.GetSqlStringCommond(strSQL);
        DataSet ds = access.ExecuteDataSet(cmd);

        strSQL = @"SELECT DocID,OrgCode,DocDate,DocT,DocTypeCode,DocTypeName,NowTime,ExcuteState,DocNo,MoCode,BZ 
                        FROM ERP_WMS_CRK WHERE DocT='出库' AND ExcuteState=0 AND OrgCode='" + ds.Tables[0].Rows[0][0].ToString() + "'";
        cmd = access_ERP.GetSqlStringCommond(strSQL);
        ds = access_ERP.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                using (Trans t = new Trans())
                {
                    try
                    {
                        //主表
                        iCount = Bind_ERP(t, ds.Tables[0].Rows[i]["DocNo"].ToString().Trim(), ds.Tables[0].Rows[i]["bz"].ToString().Trim());


                        strSQL = @"SELECT ZBJLH,WZBH,WLMC,WLGG,TXM,PH,SCRQ,SL,BZ,WareHsCode,WareHsName FROM dbo.ERP_WMS_CRK2 WHERE ZBJLH='" + ds.Tables[0].Rows[i]["DocID"].ToString().Trim() + "'";
                        cmd = access_ERP.GetSqlStringCommond(strSQL);
                        DataSet ds_Info = access_ERP.ExecuteDataSet(cmd);

                        for (int j = 0; j < ds_Info.Tables[0].Rows.Count; j++)
                        {
                            strSec_Mat_ID = getSec_Mat_ID(ds_Info.Tables[0].Rows[j]["wzbh"].ToString().Trim(), Request.Cookies["WCode"].Value.ToString().Trim(), ds_Info.Tables[0].Rows[j]["WareHsCode"].ToString().Trim(), ds_Info.Tables[0].Rows[j]["WareHsName"].ToString().Trim());
                            if (strSec_Mat_ID != "0")
                            {
                                //明细
                                Bind_ERP_Info(iCount, t, ds.Tables[0].Rows[i]["DocNo"].ToString().Trim(), ds_Info.Tables[0].Rows[j]["wzbh"].ToString().Trim(), Convert.ToDecimal(ds_Info.Tables[0].Rows[j]["SL"].ToString().Trim()), strSec_Mat_ID, ds_Info.Tables[0].Rows[j]["WareHsCode"].ToString().Trim(), ds_Info.Tables[0].Rows[j]["WareHsName"].ToString().Trim());
                                iFlag = 1;
                            }
                        }

                        t.Commit();
                    }
                    catch (Exception ex)
                    {
                        strErr = ex.Message.ToString();
                        iFlag = 0;
                        t.RollBack();
                    }

                    strErr = strErr.Replace("'", " ");

                    if (iFlag == 1)
                    {
                        //反馈
                        strSQL = @"UPDATE ERP_WMS_CRK SET ExcuteState='" + iFlag + "',fksj='" + DateTime.Now.ToString() + "',bz='" + strErr + "' WHERE DocID='" + ds.Tables[0].Rows[i]["DocID"].ToString().Trim() + "'";
                    }
                    else
                    {
                        strSQL = @"UPDATE ERP_WMS_CRK SET ExcuteState='3',fksj='" + DateTime.Now.ToString() + "',bz='物料都不存在于立体库' WHERE DocID='" + ds.Tables[0].Rows[i]["DocID"].ToString().Trim() + "'";
                    }
                    cmd = access_ERP.GetSqlStringCommond(strSQL);
                    access_ERP.ExecuteNonQuery(cmd);
                }
            }
        }
        else
        {
            ShowBox.MessageBox(this.Page, "没有数据！");
            return;
        }

        if (iFlag == 1)
        {
            ShowBox.MessageBox(this.Page, "导入成功！");
            return;
        }
        else
        {
            ShowBox.MessageBox(this.Page, "导入失败！");
            return;
        }
    }

    protected int Bind_ERP(Trans t, string strStockNo, string strContents)
    {
        int iCount = 0;
        strSQL = "Select StockNo From TBill_DelStorage where StockNo = '" + strStockNo.Trim() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        DataSet ds_Add = access.ExecuteDataSet(cmd);
        if (ds_Add.Tables[0].Rows.Count > 0)
        {
            iCount = -1;//订单编号存在
        }
        else
        {
            strSQL = "INSERT INTO TBill_DelStorage(StockNo,Small_BillType,Contents,Bill_Status,OID) values('" + strStockNo + "','11','" + strContents + "','0','" + Request.Cookies["vNovaUserNo"].Value.ToString() + "')";
            cmd = access.GetSqlStringCommond(strSQL);
            if (t == null)
            {
                access.ExecuteNonQuery(cmd);
            }
            else
            {
                access.ExecuteNonQuery(cmd, t);
            }
        }
        return iCount;
    }

    public void Bind_ERP_Info(int iCount, Trans t, string strStockNo, string strMat_Code, decimal dNum, string strSec_Mat_ID, string strWCode, string strWName)
    {
        Random rad = new Random();
        string strW01 = strStockNo + rad.Next(1000, 9999);

        strSQL = @"Insert Into TDelStorage_List(StockNo,Mat_Code,SEC_MAT_ID,Num,W01,ACT_NUM,TS02,TS03)
                                    values('" + strStockNo + "','" + strMat_Code + "','" + strSec_Mat_ID + "','" + dNum + "','" + strW01 + "',0,'" + strWCode.Trim() + "','" + strWName.Trim() + "')";
        cmd = access.GetSqlStringCommond(strSQL);
        if (t == null)
        {
            access.ExecuteNonQuery(cmd);
        }
        else
        {
            access.ExecuteNonQuery(cmd, t);
        }
    }



    /// <summary>
    /// 生成二级编码
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    protected string getSec_Mat_ID(string strMat_Code, string strUserName, string strWCode, string strWName)
    {
        int iSecs = 0;
        try
        {
            cmd = access.GetStoredProcCommond("SP_Get_Sec_Mat_ID");
            access.AddInParameter(cmd, "@Mat_Code", DbType.String, strMat_Code);
            access.AddInParameter(cmd, "@D01", DbType.String, "");
            access.AddInParameter(cmd, "@D02", DbType.String, "");
            access.AddInParameter(cmd, "@D03", DbType.String, "");
            access.AddInParameter(cmd, "@D04", DbType.String, "");
            access.AddInParameter(cmd, "@D05", DbType.String, "");
            access.AddInParameter(cmd, "@D06", DbType.String, "");
            access.AddInParameter(cmd, "@D07", DbType.String, "");
            access.AddInParameter(cmd, "@D08", DbType.String, "");
            access.AddInParameter(cmd, "@D09", DbType.String, "");
            access.AddInParameter(cmd, "@D10", DbType.String, "");
            access.AddInParameter(cmd, "@S01", DbType.String, "");
            access.AddInParameter(cmd, "@S02", DbType.String, "");
            //access.AddInParameter(cmd, "@S01", DbType.String, strWCode.Trim());
            //access.AddInParameter(cmd, "@S02", DbType.String, strWName.Trim());
            access.AddInParameter(cmd, "@S03", DbType.String, "");
            access.AddInParameter(cmd, "@S04", DbType.String, "");
            access.AddInParameter(cmd, "@S05", DbType.String, "");
            access.AddInParameter(cmd, "@S06", DbType.String, "");
            access.AddInParameter(cmd, "@S07", DbType.String, "");
            access.AddInParameter(cmd, "@S08", DbType.String, "");
            access.AddInParameter(cmd, "@S09", DbType.String, "");
            access.AddInParameter(cmd, "@S10", DbType.String, "");
            access.AddInParameter(cmd, "@S11", DbType.String, "");
            access.AddInParameter(cmd, "@S12", DbType.String, "");
            access.AddInParameter(cmd, "@S13", DbType.String, "");
            access.AddInParameter(cmd, "@S14", DbType.String, "");
            access.AddInParameter(cmd, "@S15", DbType.String, "");
            access.AddInParameter(cmd, "@S16", DbType.String, "");
            access.AddInParameter(cmd, "@S17", DbType.String, "");
            access.AddInParameter(cmd, "@S18", DbType.String, "");
            access.AddInParameter(cmd, "@S19", DbType.String, "");
            access.AddInParameter(cmd, "@S20", DbType.String, "");
            access.AddInParameter(cmd, "@N01", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N02", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N03", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N04", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N05", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N06", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N07", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N08", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N09", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@N10", DbType.Decimal, 0);
            access.AddInParameter(cmd, "@DynamicState01", DbType.Int16, 0);
            access.AddInParameter(cmd, "@DynamicState02", DbType.Int16, 0);
            access.AddInParameter(cmd, "@DynamicState03", DbType.Int16, 0);
            access.AddInParameter(cmd, "@DynamicState04", DbType.Int16, 0);
            access.AddInParameter(cmd, "@DynamicState05", DbType.Int16, 0);
            access.AddInParameter(cmd, "@TS01", DbType.String, "");
            access.AddInParameter(cmd, "@TS02", DbType.String, "");
            access.AddInParameter(cmd, "@TS03", DbType.String, "");
            access.AddInParameter(cmd, "@TS04", DbType.String, "");
            access.AddInParameter(cmd, "@TS05", DbType.String, "");
            access.AddInParameter(cmd, "@ForceNew", DbType.Int16, 1);
            access.AddInParameter(cmd, "@UserName", DbType.String, strUserName);
            access.AddOutParameter(cmd, "@Sec_Mat_ID", DbType.Int32, 20);
            access.AddOutParameter(cmd, "@ResNO", DbType.Int16, 10);
            access.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
            access.ExecuteNonQuery(cmd);
            iSecs = Convert.ToInt32(access.GetParameter(cmd, "@Sec_Mat_ID").Value.ToString());

        }
        catch (Exception ex)
        {
            iSecs = 0;
        }

        string strSecs = iSecs.ToString();
        return strSecs;
    }
}

